package com.tinysand.advert.sender.impl;

import com.alibaba.fastjson.JSON;
import com.tinysand.advert.handler.AdLevelDataHandler;
import com.tinysand.advert.mysql.constant.Constant;
import com.tinysand.advert.mysql.constant.DataLevel;
import com.tinysand.advert.mysql.dto.MySqlRowData;
import com.tinysand.advert.sender.Sender;
import com.tinysand.advert.support.*;
import com.tinysand.advert.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author TINY
 */
@Slf4j
@Component("indexSender")
public class IndexSender implements Sender {

    @Override
    public void send(MySqlRowData rowData) {
        String level = rowData.getLevel();

        if (DataLevel.LEVEL2.getLevel().equals(level)) {
            level2RowData(rowData);
        } else if (DataLevel.LEVEL3.getLevel().equals(level)) {
            level3RowData(rowData);
        } else if (DataLevel.LEVEL4.getLevel().equals(level)) {
            level4RowData(rowData);
        } else {
            log.error("不存在的数据层级，{}", JSON.toJSONString(rowData));
        }
    }

    private void level2RowData(MySqlRowData rowData) {
        if (rowData.getTableName().equals(
                Constant.AD_PLAN_TABLE_INFO.TABLE_NAME
        )) {
            List<AdPlanTable> planTables = new ArrayList<>();
            for (Map<String, String> fieldValueMap :
                    rowData.getFieldValueMap()) {
                AdPlanTable planTable = new AdPlanTable();

                fieldValueMap.forEach((k, v) -> {
                    switch (k) {
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_ID:
                            planTable.setId(Long.valueOf(v));
                            break;

                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_USER_ID:
                            planTable.setUserId(Long.valueOf(v));
                            break;

                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_PLAN_STATUS:
                            planTable.setPlanStatus(Integer.valueOf(v));
                            break;

                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_START_DATE:
                            planTable.setStartDate(
                                    CommonUtils.parseString2Date(v)
                            );
                            break;

                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_END_DATE:
                            planTable.setEndDate(
                                    CommonUtils.parseString2Date(v)
                            );
                            break;
                    }
                });
                planTables.add(planTable);
            }
            planTables.forEach(p -> AdLevelDataHandler.handleLevel2(
                    p, rowData.getOperationType()));
        } else if (rowData.getTableName().equals(
                Constant.AD_CREATIVE_TABLE_INFO.TABLE_NAME
        )) {
            List<AdvertTable> advertTables = new ArrayList<>();
            for (Map<String, String> fieldValueMap :
                    rowData.getFieldValueMap()) {
                AdvertTable advertTable = new AdvertTable();

                fieldValueMap.forEach((k, v) -> {
                    switch (k) {
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_ID:
                            advertTable.setAdId(Long.valueOf(v));
                            break;

                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_TYPE:
                            advertTable.setType(Integer.valueOf(v));
                            break;

                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_MATERIAL_TYPE:
                            advertTable.setMaterialType(Integer.valueOf(v));
                            break;

                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_HEIGHT:
                            advertTable.setHeight(Integer.valueOf(v));
                            break;

                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_WIDTH:
                            advertTable.setWidth(Integer.valueOf(v));
                            break;

                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_AUDIT_STATUS:
                            advertTable.setAuditStatus(Integer.valueOf(v));
                            break;

                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_URL:
                            advertTable.setAdUrl(String.valueOf(v));
                            break;
                    }
                });
                advertTables.add(advertTable);
            }
            advertTables.forEach(a -> AdLevelDataHandler.handleLevel2(
                    a, rowData.getOperationType()));
        }
    }

    private void level3RowData(MySqlRowData rowData) {
        if (rowData.getTableName().equals(
                Constant.AD_UNIT_TABLE_INFO.TABLE_NAME
        )) {
            List<AdUnitTable> adUnitTables = new ArrayList<>();
            for (Map<String, String> fieldValueMap :
                    rowData.getFieldValueMap()) {
                AdUnitTable adUnitTable = new AdUnitTable();

                fieldValueMap.forEach((k, v) -> {
                    switch (k) {
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_ID:
                            adUnitTable.setUnitId(Long.valueOf(v));
                            break;

                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_UNIT_STATUS:
                            adUnitTable.setUnitStatus(Integer.valueOf(v));
                            break;

                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_POSITION_TYPE:
                            adUnitTable.setPositionType(Integer.valueOf(v));
                            break;

                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_PLAN_ID:
                            adUnitTable.setPlanId(Long.valueOf(v));
                            break;
                    }
                });
                adUnitTables.add(adUnitTable);
            }
            adUnitTables.forEach(u -> AdLevelDataHandler.handleLevel3(
                    u, rowData.getOperationType()));
        } else if (rowData.getTableName().equals(
                Constant.AD_CREATIVE_UNIT_TABLE_INFO.TABLE_NAME
        )) {
            List<AdUnitRelTable> adUnitRelTables = new ArrayList<>();
            for (Map<String, String> fieldValueMap :
                    rowData.getFieldValueMap()) {
                AdUnitRelTable adUnitRelTable = new AdUnitRelTable();

                fieldValueMap.forEach((k, v) -> {
                    switch (k) {
                        case Constant.AD_CREATIVE_UNIT_TABLE_INFO.COLUMN_CREATIVE_ID:
                            adUnitRelTable.setAdId(Long.valueOf(v));
                            break;

                        case Constant.AD_CREATIVE_UNIT_TABLE_INFO.COLUMN_UNIT_ID:
                            adUnitRelTable.setUnitId(Long.valueOf(v));
                            break;
                    }
                });
                adUnitRelTables.add(adUnitRelTable);
            }
            adUnitRelTables.forEach(au -> AdLevelDataHandler.handleLevel3(
                    au, rowData.getOperationType()));
        }
    }

    private void level4RowData(MySqlRowData rowData) {
        switch (rowData.getTableName()) {
            case Constant.AD_UNIT_DISTRICT_TABLE_INFO.TABLE_NAME:
                List<DistrictTable> districtTables = new ArrayList<>();
                for (Map<String, String> fieldValueMap :
                        rowData.getFieldValueMap()) {
                    DistrictTable districtTable = new DistrictTable();

                    fieldValueMap.forEach((k, v) -> {
                        switch (k) {
                            case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_UNIT_ID:
                                districtTable.setUnitId(Long.valueOf(v));
                                break;

                            case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_PROVINCE:
                                districtTable.setProvince(String.valueOf(v));
                                break;

                            case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_CITY:
                                districtTable.setCity(String.valueOf(v));
                                break;
                        }
                    });
                    districtTables.add(districtTable);
                }
                districtTables.forEach(d -> AdLevelDataHandler.handleLevel4(
                        d, rowData.getOperationType()));
                break;
            case Constant.AD_UNIT_IT_TABLE_INFO.TABLE_NAME:
                List<InterestTable> interestTables = new ArrayList<>();
                for (Map<String, String> fieldValueMap :
                        rowData.getFieldValueMap()) {
                    InterestTable interestTable = new InterestTable();

                    fieldValueMap.forEach((k, v) -> {
                        switch (k) {
                            case Constant.AD_UNIT_IT_TABLE_INFO.COLUMN_UNIT_ID:
                                interestTable.setUnitId(Long.valueOf(v));
                                break;

                            case Constant.AD_UNIT_IT_TABLE_INFO.COLUMN_IT_TAG:
                                interestTable.setItTag(String.valueOf(v));
                                break;
                        }
                    });
                    interestTables.add(interestTable);
                }
                interestTables.forEach(i -> AdLevelDataHandler.handleLevel4(
                        i, rowData.getOperationType()));
                break;
            case Constant.AD_UNIT_KEYWORD_TABLE_INFO.TABLE_NAME:
                List<KeywordTable> keywordTables = new ArrayList<>();
                for (Map<String, String> fieldValueMap :
                        rowData.getFieldValueMap()) {
                    KeywordTable keywordTable = new KeywordTable();

                    fieldValueMap.forEach((k, v) -> {
                        switch (k) {
                            case Constant.AD_UNIT_KEYWORD_TABLE_INFO.COLUMN_UNIT_ID:
                                keywordTable.setUnitId(Long.valueOf(v));
                                break;

                            case Constant.AD_UNIT_KEYWORD_TABLE_INFO.COLUMN_KEYWORD:
                                keywordTable.setKeyword(String.valueOf(v));
                                break;
                        }
                    });
                    keywordTables.add(keywordTable);
                }
                keywordTables.forEach(k -> AdLevelDataHandler.handleLevel4(
                        k, rowData.getOperationType()));
                break;
        }
    }
}
